﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace LVYAN.Utils
{
    /// <summary>
    /// 
    /// </summary>
    public class LoggerService
    {
        protected readonly int _loggerLevel = 10;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ClassName">类名称</param>
        /// <param name="Action">方法名称</param>
        /// <param name="Key">自定义</param>
        /// <param name="Data">存储数据</param>
        public void ResponseMsgDebug(string Moudle, string Key, string Data)
        {
            if (_loggerLevel >= 1)
                WriteLog(Moudle, nameof(ResponseMsgDebug), Key, Data);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ClassName">类名称</param>
        /// <param name="Action">方法名称</param>
        /// <param name="Key">自定义</param>
        /// <param name="Data">存储数据</param>
        public void RequestMsgDebug(string Moudle, string Key, string Data)
        {
            if (_loggerLevel >= 1)
                WriteLog(Moudle, nameof(RequestMsgDebug), Key, Data);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ClassName">类名称</param>
        /// <param name="Action">方法名称</param>
        /// <param name="Key">自定义</param>
        /// <param name="Data">存储数据</param>
        public void ResponseMsgInfo(string Moudle, string Key, string Data)
        {
            if (_loggerLevel >= 2)
                WriteLog(Moudle, nameof(ResponseMsgInfo), Key, Data);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ClassName">类名称</param>
        /// <param name="Action">方法名称</param>
        /// <param name="Key">自定义</param>
        /// <param name="Data">存储数据</param>
        public void RequestMsgInfo(string Moudle, string Key, string Data)
        {
            if (_loggerLevel >= 2)
                WriteLog(Moudle, nameof(RequestMsgInfo), Key, Data);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ClassName">类名称</param>
        /// <param name="Action">方法名称</param>
        /// <param name="Key">自定义</param>
        /// <param name="Data">存储数据</param>
        public void ResponseMsgError(string Moudle, string Key, string Data)
        {
            if (_loggerLevel >= 3)
                WriteLog(Moudle, nameof(ResponseMsgError), Key, Data);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ClassName">类名称</param>
        /// <param name="Action">方法名称</param>
        /// <param name="Key">自定义</param>
        /// <param name="Data">存储数据</param>
        public void RequestMsgError(string Moudle, string Key, string Data)
        {
            if (_loggerLevel >= 3)
                WriteLog(Moudle, nameof(RequestMsgError), Key, Data);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ClassName">类名称</param>
        /// <param name="Action">方法名称</param>
        /// <param name="Key">自定义</param>
        /// <param name="Data">存储数据</param>
        public void ResponseMsgDebug<T>(string Moudle, string Key, T Data) where T : class
        {
            if (_loggerLevel >= 1)
                WriteLog(Moudle, nameof(ResponseMsgDebug), Key, JsonConvert.SerializeObject(Data));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ClassName">类名称</param>
        /// <param name="Action">方法名称</param>
        /// <param name="Key">自定义</param>
        /// <param name="Data">存储数据</param>
        public void RequestMsgDebug<T>(string Moudle, string Key, T Data) where T : class
        {
            if (_loggerLevel >= 1)
                WriteLog(Moudle, nameof(RequestMsgDebug), Key, JsonConvert.SerializeObject(Data));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ClassName">类名称</param>
        /// <param name="Action">方法名称</param>
        /// <param name="Key">自定义</param>
        /// <param name="Data">存储数据</param>
        public void ResponseMsgInfo<T>(string Moudle, string Key, T Data) where T : class
        {
            if (_loggerLevel >= 2)
                WriteLog(Moudle, nameof(ResponseMsgInfo), Key, JsonConvert.SerializeObject(Data));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ClassName">类名称</param>
        /// <param name="Action">方法名称</param>
        /// <param name="Key">自定义</param>
        /// <param name="Data">存储数据</param>
        public void RequestMsgInfo<T>(string Moudle, string Key, T Data) where T : class
        {
            if (_loggerLevel >= 2)
                WriteLog(Moudle, nameof(RequestMsgInfo), Key, JsonConvert.SerializeObject(Data));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ClassName">类名称</param>
        /// <param name="Action">方法名称</param>
        /// <param name="Key">自定义</param>
        /// <param name="Data">存储数据</param>
        public void ResponseMsgError<T>(string Moudle, string Key, T Data) where T : class
        {
            if (_loggerLevel >= 3)
                WriteLog(Moudle, nameof(ResponseMsgError), Key, JsonConvert.SerializeObject(Data));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ClassName">类名称</param>
        /// <param name="Action">方法名称</param>
        /// <param name="Key">自定义</param>
        /// <param name="Data">存储数据</param>
        public void RequestMsgError<T>(string Moudle, string Key, T Data) where T : class
        {
            if (_loggerLevel >= 3)
                WriteLog(Moudle, nameof(RequestMsgError), Key, JsonConvert.SerializeObject(Data));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ClassName">类名称</param>
        /// <param name="Action">方法名称</param>
        /// <param name="Key">自定义</param>
        /// <param name="Data">存储数据</param>
        protected static void WriteLog(string Moudle, string Type, string Key, string Data)
        {
            var Model = new AIConfig().GetConfigModel(nameof(LoggerService), nameof(WriteLog));
            if (!Model.IsLog) return;
            try
            {
                try
                {
                    if (string.IsNullOrWhiteSpace(Data)) return;
                   
                   var dic= JsonService.ToModel<Dictionary<string,object>>(Data);
                    if (dic.ContainsKey("RobotAvatar")) dic["RobotAvatar"] = "";
                    if (dic.ContainsKey("RoomAvatar")) dic["RoomAvatar"] = "";
                    if (dic.ContainsKey("NickAvatar")) dic["NickAvatar"] = "";
                    Data = JsonService.ToString(dic);
                }
                catch (Exception ex)
                {
                    
                }

                //获取当前系统时间
                string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                //日志内容
                string write_content = $"\r\n\r\n\r\n  {time} {Key} \r\n\r\n  {Data} ";
                //需要用户自定义日志实现形式
                if (Model.IsConsole)
                {
                    Task.Run(() =>
                    {
                        Console.WriteLine(write_content);
                        Console.In.ReadToEnd(); // 清除控制台输入缓冲区
                    });
                }
                string fileName = DateTime.Now.ToString("yyyyMMddHH") + ".txt";
                string urlPath = $"Logs/{ DateTime.Now.ToString("yyyyMMdd")}/{Moudle}/{Type}/" + fileName; // 
                string filePath = AppDomain.CurrentDomain.BaseDirectory + urlPath; // 文件路径
                string directoryName = Path.GetDirectoryName(filePath);
                if (!Directory.Exists(directoryName))
                {
                    Directory.CreateDirectory(directoryName);
                }
                FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write);
                //获得字节数组
                byte[] data = System.Text.Encoding.Default.GetBytes(write_content);
                //开始写入
                fs.Write(data, 0, data.Length);
                //清空缓冲区、关闭流
                fs.Flush();
                fs.Close();
            }
            catch (Exception ex)
            {
                WriteLog1(Moudle, Type, Key, $"{Data}_{ex.Message}");
            }
        }
        protected static void WriteLog1(string Moudle, string Type, string Key, string Data)
        {
            try
            {
                //获取当前系统时间
                string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                //日志内容
                string write_content = $"\r\n\r\n\r\n  {time} {Key} \r\n\r\n  {Data} ";
                //需要用户自定义日志实现形式
                Task.Run(() =>
                {
                    Console.WriteLine(write_content);
                    Console.In.ReadToEnd(); // 清除控制台输入缓冲区
                });
                string fileName = DateTime.Now.ToString("yyyyMMddHH") + ".txt";
                string urlPath = $"LogsException/{ DateTime.Now.ToString("yyyyMMdd")}/{Moudle}/{Type}/{Key}/" + fileName; // 
                string filePath = AppDomain.CurrentDomain.BaseDirectory + urlPath; // 文件路径
                string directoryName = Path.GetDirectoryName(filePath);
                if (!Directory.Exists(directoryName))
                {
                    Directory.CreateDirectory(directoryName);
                }
                FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write);
                //获得字节数组
                byte[] data = System.Text.Encoding.Default.GetBytes(write_content);
                //开始写入
                fs.Write(data, 0, data.Length);
                //清空缓冲区、关闭流
                fs.Flush();
                fs.Close();
            }
            catch (Exception ex)
            {


            }
        }
    }
}